import "./index.css";
import { useState } from "react";


function Game() {
    const [xIsNext, setXIsNext] = useState(true);
    const [history, setHistory] = useState([Array(9).fill(null)]);
    const [currentMove, setCurrentMove] = useState(0);
    console.log(history);
    
    // const value = history[history.length - 1];
    const value = history[currentMove];

    function handlePlay(nextSquares) {
        // TODO
        const nextHistory = [...history.slice(0, currentMove + 1), nextSquares];
        setHistory(nextHistory);
        setCurrentMove(nextHistory.length - 1);
        setXIsNext(!xIsNext);
      }

      const moves=history.map((item,index)=>{
        let desc;
        if(index>0){
            desc='go to move #'+index
        }else{
            desc='go to game start'
        }
        return <li  key={index}> 
            <button onClick={()=>JumpTo(index)}>{desc}</button>
        </li>
      })
      const JumpTo=(nextMove)=>{
        setCurrentMove(nextMove);
        setXIsNext(nextMove % 2 === 0);
      }

    return (
        <div className="game-box">
          <div className="game-board">
            <Board isNext={xIsNext} value={value} onPlay={handlePlay} />
          </div>
          <div className="game-info">
            <ol>{moves}</ol>
          </div>
        </div>
      );
}
function Board({isNext,value,onPlay}) {
//   const [value, setValue] = useState(Array(9).fill(null));
//   const [isNext, setIsNext] = useState(true);


  const handleSet = (i) => {
    let sliceData = value.slice();

    if(sliceData[i-1] || calculateWinner(value))return

    console.log(sliceData);
    if(isNext){
        sliceData[i-1] = "X";
    }else{
        sliceData[i-1] = "O";
    }

    onPlay(sliceData);
  };
  let status;
  console.log(value);
  if(value){
    const winner = calculateWinner(value);
    if (winner) {
      status = "Winner: " + winner;
    } else {
      status = "Next player: " + (isNext ? "X" : "O");
    }
  } 
  

  return (
    <div className="game">
        <div>{status}</div>
      <div className="row">
        <Square value={value[0]} handle={() => handleSet(1)} />
        <Square value={value[1]} handle={() => handleSet(2)} />
        <Square value={value[2]} handle={() => handleSet(3)} />
      </div>
      <div className="row">
        <Square value={value[3]} handle={() => handleSet(4)} />
        <Square value={value[4]} handle={() => handleSet(5)} />
        <Square value={value[5]} handle={() => handleSet(6)} />
      </div>
      <div className="row">
        <Square value={value[6]} handle={() => handleSet(7)} />
        <Square value={value[7]} handle={() => handleSet(8)} />
        <Square value={value[8]} handle={() => handleSet(9)} />
      </div>
    </div>
  );
}

function Square({ value, handle }) {
    return (
      <div className="square" onClick={handle}>
        {value}
      </div>
    );
  }
//判断胜负
function calculateWinner(squares) {
    const lines = [
      [0, 1, 2],
      [3, 4, 5],
      [6, 7, 8],
      [0, 3, 6],
      [1, 4, 7],
      [2, 5, 8],
      [0, 4, 8],
      [2, 4, 6]
    ];
    for (let i = 0; i < lines.length; i++) {
      const [a, b, c] = lines[i];
      if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
        return squares[a];
      }
    }
    return null;
  }

export default Game;
